<template>
	<view class="v_body">
		<uni-title type="h1" title="修改个人信息" align="center"></uni-title>
		<view class="">
			<uni-forms :modelValue="formData" ref="form" :rules="rules">
				<uni-forms-item required label="用户名" name="username">
					<uni-easyinput type="text" v-model="formData.username" placeholder="请输入用户名" disabled />
				</uni-forms-item>
				<uni-forms-item required name="email" label="邮箱">
					<uni-easyinput type="text" v-model="formData.email" placeholder="请输入邮箱" />
				</uni-forms-item>
				<uni-forms-item required name="phone" label="手机号">
					<uni-easyinput type="text" v-model="formData.phone" placeholder="请输入手机号" />
				</uni-forms-item>
			</uni-forms>
			<button @click="updateInfo">修改</button>
		</view>
		
		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msg.msgType" :message="msg.messageText" :duration="2000"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import { getUserById, updateUser } from '../../api/person.js'
	export default {
		data() {
			return {
				uid: this.$utils.getStorage('uid'),
				formData: {
					username: '',
					email: '',
					phone: ''
				},
				// 表单校验规则
				rules: {
				},
				msg: {}
			}
		},
		onLoad() {
			// this.formData = this.$utils.getStorage('user')
			this.getUserInfo()
		},
		onShow() {
			// this.formData = this.$utils.getStorage('user')
			this.getUserInfo()
		},
		methods: {
			getUserInfo(){
				getUserById(this.uid).then(res => {
					if(res.code === 200){
						this.formData = res.data
					}else{
						this.msg = this.$utils.messageError(res.msg)
						this.$refs.message.open()
					}
				})
			},
			updateInfo(){
				updateUser(this.formData).then(res => {
					if(res.code === 200){
						this.formData = res.data
						this.msg = this.$utils.messageSuccess(res.msg)
						this.$refs.message.open()
						uni.switchTab({
							url:'person'
						})
					}else{
						this.msg = this.$utils.messageError(res.msg)
						this.$refs.message.open()
					}
				}).catch(reason => {
					this.msg = this.$utils.messageError(reason)
					this.$refs.message.open()
				})
			}
		}
	}
</script>

<style>

</style>
